package com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan;

import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.DingAdapter01;
import com.lwj.liwanjia.adapter.DingDanTypeAdapter;
import com.lwj.liwanjia.adapter.KuaiDiXuanZeAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.DingBean02;
import com.lwj.liwanjia.bean.DingBean03;
import com.lwj.liwanjia.bean.DingDanTypeBean;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.SpfUtils;

import java.io.IOException;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * Created by 001 on 2017/9/22.
 */

public class DaiFaHuoXiangQingActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(DaiFaHuoXiangQingActivity.this, "数据有误", Toast.LENGTH_LONG).show();
                    break;

                case 1:

                    Toast.makeText(DaiFaHuoXiangQingActivity.this, "发货成功", Toast.LENGTH_LONG).show();
                    finish();

                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private RecyclerView mRecyclerView;
    private DingDanTypeAdapter typeAdapter;
    private List<DingDanTypeBean> typeList;
    private RelativeLayout rl_kuaiDiXuanZe;//快递选择
    private RecyclerView kuaiDiRecyclerView;//快递 容器
    private Dialog kuaiDiDialog;//快递的Dialog
    private KuaiDiXuanZeAdapter kuaiDiAdapter;//快递适配器
    private List<String> kuaiDiList;//快递数据集合
    private Button bt_cancel;//快递 关闭
    private TextView tvLiJiFaHuo;//立即发货

    private List<DingBean02.DataBean.CommodityBean> homeList;
    private DingAdapter01 homeAdapter;
    private CircleImageView circle_photo;
    private TextView tv_name;
    private TextView tv_telephone;
    private TextView tv_address;
    private TextView tv_kuaiDi;
    private RelativeLayout rl_01;//顺风
    private RelativeLayout rl_02;//圆通
    private ImageView iv_01;//顺风
    private ImageView iv_02;//圆通
    private String kuaiDiType = "顺丰快递";
    private EditText et_kuaiDiDanHao;//快递单号
    private EditText et_beiZhu;//备注
    private String dingDanHao;//订单号
    private RelativeLayout rl_03;
    private RelativeLayout rl_04;
    private RelativeLayout rl_05;
    private RelativeLayout rl_06;
    private RelativeLayout rl_07;
    private RelativeLayout rl_08;
    private ImageView iv_03;
    private ImageView iv_04;
    private ImageView iv_05;
    private ImageView iv_06;
    private ImageView iv_07;
    private ImageView iv_08;
    private ImageView iv_09;
    private TextView tvName1;

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        rl_kuaiDiXuanZe = (RelativeLayout) findViewById(R.id.rl_kuaiDiXuanZe);
        tvLiJiFaHuo = (TextView) findViewById(R.id.tv_liJiFaHuo);

        circle_photo = (CircleImageView) findViewById(R.id.circle_photo);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_telephone = (TextView) findViewById(R.id.tv_telephone);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_kuaiDi = (TextView) findViewById(R.id.tv_kuaiDi);
        et_kuaiDiDanHao = (EditText) findViewById(R.id.et_kuaiDiDanHao);
        et_beiZhu = (EditText) findViewById(R.id.et_beiZhu);
        tvName1 = (TextView) findViewById(R.id.tv_name1);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("发货");

        tv_back.setOnClickListener(this);
        rl_kuaiDiXuanZe.setOnClickListener(this);
        tvLiJiFaHuo.setOnClickListener(this);
    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        String photoUrl = intent.getStringExtra("photoUrl");
        String name = intent.getStringExtra("name");
        String name1 = intent.getStringExtra("name1");
        String telephone = intent.getStringExtra("telephone");
        String address = intent.getStringExtra("address");
        dingDanHao = intent.getStringExtra("dingDanHao");
        List<DingBean02.DataBean.CommodityBean> homeList = (List<DingBean02.DataBean.CommodityBean>) intent.getSerializableExtra("homeList");
        if (homeList != null && homeList.size() >= 0) {
            LinearLayoutManager manager = new LinearLayoutManager(DaiFaHuoXiangQingActivity.this);
            mRecyclerView.setLayoutManager(manager);
            homeAdapter = new DingAdapter01(DaiFaHuoXiangQingActivity.this, homeList);
            mRecyclerView.setAdapter(homeAdapter);
        }

        if (!TextUtils.isEmpty(photoUrl)) {
            Glide.with(this).load(photoUrl).error(R.mipmap.touxiang).into(circle_photo);
        }

        if (!TextUtils.isEmpty(name)) {
            tv_name.setText(name);
        }
        if (!TextUtils.isEmpty(name1)) {
            tvName1.setText(name1);
        }

        if (!TextUtils.isEmpty(telephone)) {
            tv_telephone.setText(telephone);
        }

        if (!TextUtils.isEmpty(address)) {
            tv_address.setText(address);
        }

        //快递的Dialog
        initTianJiaDialog();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dai_fa_huo_xiang_qing;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
            //快递选择
            case R.id.rl_kuaiDiXuanZe:
                kuaiDiDialog.show();
                break;
            //顺丰
            case R.id.rl_shunFeng:
                kuaiDiType = "顺丰快递";
                changeKuaiDiUi(iv_01, iv_02, iv_03, iv_04, iv_05, iv_06, iv_07, iv_08);
                break;
            //圆通
            case R.id.rl_yuanTong:
                kuaiDiType = "圆通快递";
                changeKuaiDiUi(iv_02, iv_01, iv_03, iv_04, iv_05, iv_06, iv_07, iv_08);
                break;
            //申通
            case R.id.rl_shenTong:
                kuaiDiType = "申通快递";
                changeKuaiDiUi(iv_03, iv_02, iv_01, iv_04, iv_05, iv_06, iv_07, iv_08);
                break;
            //韵达
            case R.id.rl_04:
                kuaiDiType = "韵达快递";
                changeKuaiDiUi(iv_04, iv_02, iv_03, iv_01, iv_05, iv_06, iv_07, iv_08);
                break;
            //天天
            case R.id.rl_05:
                kuaiDiType = "天天快递";
                changeKuaiDiUi(iv_05, iv_02, iv_03, iv_04, iv_01, iv_06, iv_07, iv_08);
                break;
            //中通
            case R.id.rl_06:
                kuaiDiType = "中通快递";
                changeKuaiDiUi(iv_06, iv_02, iv_03, iv_04, iv_05, iv_01, iv_07, iv_08);
                break;
            //汇通
            case R.id.rl_07:
                kuaiDiType = "汇通快递";
                changeKuaiDiUi(iv_07, iv_02, iv_03, iv_04, iv_05, iv_06, iv_01, iv_08);
                break;
            //德邦
            case R.id.rl_08:
                kuaiDiType = "德邦快递";
                changeKuaiDiUi(iv_08, iv_02, iv_03, iv_04, iv_05, iv_06, iv_07, iv_01);
                break;

            //Dialog 关闭
            case R.id.bt_cancel:
                tv_kuaiDi.setText(kuaiDiType);
                kuaiDiDialog.dismiss();
                break;
            //立即发货
            case R.id.tv_liJiFaHuo:

                String kuaiDi = tv_kuaiDi.getText().toString().trim();
                String kuaiDiDanHao = et_kuaiDiDanHao.getText().toString().trim();
                String beiZHu = et_beiZhu.getText().toString().trim();

                if (!TextUtils.isEmpty(kuaiDi)
                        && !TextUtils.isEmpty(kuaiDiDanHao)
                        && !TextUtils.isEmpty(dingDanHao)) {
                    //请求网络 上传数据
                    initOkHttp(kuaiDi, kuaiDiDanHao, beiZHu);
                } else {
                    Toast.makeText(DaiFaHuoXiangQingActivity.this, "数据不能为空", Toast.LENGTH_LONG).show();
                }

                break;
        }
    }

    //快递选择的Dialog
    private void initTianJiaDialog() {

        kuaiDiDialog = new Dialog(this, R.style.DialogTheme);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_kuai_di, null);
        kuaiDiDialog.setContentView(dialogView);

        Window dialogWindow = kuaiDiDialog.getWindow();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1);
        dialogWindow.setGravity(Gravity.BOTTOM);

        bt_cancel = (Button) dialogView.findViewById(R.id.bt_cancel);
        rl_01 = (RelativeLayout) dialogView.findViewById(R.id.rl_shunFeng);
        rl_02 = (RelativeLayout) dialogView.findViewById(R.id.rl_yuanTong);
        rl_03 = (RelativeLayout) dialogView.findViewById(R.id.rl_shenTong);
        rl_04 = (RelativeLayout) dialogView.findViewById(R.id.rl_04);
        rl_05 = (RelativeLayout) dialogView.findViewById(R.id.rl_05);
        rl_06 = (RelativeLayout) dialogView.findViewById(R.id.rl_06);
        rl_07 = (RelativeLayout) dialogView.findViewById(R.id.rl_07);
        rl_08 = (RelativeLayout) dialogView.findViewById(R.id.rl_08);

        iv_01 = (ImageView) dialogView.findViewById(R.id.iv_01);
        iv_02 = (ImageView) dialogView.findViewById(R.id.iv_02);
        iv_03 = (ImageView) dialogView.findViewById(R.id.iv_03);
        iv_04 = (ImageView) dialogView.findViewById(R.id.iv_04);
        iv_05 = (ImageView) dialogView.findViewById(R.id.iv_05);
        iv_06 = (ImageView) dialogView.findViewById(R.id.iv_06);
        iv_07 = (ImageView) dialogView.findViewById(R.id.iv_07);
        iv_08 = (ImageView) dialogView.findViewById(R.id.iv_08);

        rl_01.setOnClickListener(this);
        rl_02.setOnClickListener(this);
        rl_03.setOnClickListener(this);
        rl_04.setOnClickListener(this);
        rl_05.setOnClickListener(this);
        rl_06.setOnClickListener(this);
        rl_07.setOnClickListener(this);
        rl_08.setOnClickListener(this);

        bt_cancel.setOnClickListener(this);

    }

    //更改快递UI
    private void changeKuaiDiUi(ImageView iv_01, ImageView iv_02, ImageView iv_03, ImageView iv_04, ImageView iv_05, ImageView iv_06, ImageView iv_07, ImageView iv_08) {
        iv_01.setImageResource(R.mipmap.kuang_1);
        iv_02.setImageResource(R.mipmap.kuang);
        iv_03.setImageResource(R.mipmap.kuang);
        iv_04.setImageResource(R.mipmap.kuang);
        iv_05.setImageResource(R.mipmap.kuang);
        iv_06.setImageResource(R.mipmap.kuang);
        iv_07.setImageResource(R.mipmap.kuang);
        iv_08.setImageResource(R.mipmap.kuang);
    }

    //请求网络 上传数据
    private void initOkHttp(String kuaiDi, String kuaiDiDanHao, String beiZhu) {

        String url = URLConstant.BASE_URL + "pay/send";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .add("tradeId", dingDanHao)
                .add("logisticsId", kuaiDiDanHao)
                .add("logisticsType", kuaiDi)
                .add("payinfo", beiZhu)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "分类数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "分类图数据获取成功");

                try {
                    String result = response.body().string();
                    Log.d("Tag", result);
                    parse01(result);
                } catch (Exception e) {

                }

            }
        });
    }

    //解析
    private void parse01(String result) {

        Gson gson = new Gson();
        DingBean03 bean = gson.fromJson(result, DingBean03.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:
                    mHandler.sendEmptyMessage(1);
                    break;
            }
        }
    }

}
